package com.xinsoft.controller.basic;

import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.SysMenuDTO;
import com.xinsoft.service.SysMenuService;
import com.xinsoft.validator.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/system/menu")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 添加
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/add")
    public Response<?> addMenu(@RequestBody SysMenuDTO sysMenuDTO) {
        // 参数校验
        ValidatorUtils.validateEntity(sysMenuDTO);
        return Response.succeed(sysMenuService.addMenu(sysMenuDTO));
    }

    /**
     * 编辑菜单
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/edit")
    public Response<?> edit(@RequestBody  SysMenuDTO sysMenuDTO) {
        // 参数校验
        ValidatorUtils.validateEntity(sysMenuDTO);
        return Response.succeed(sysMenuService.editSysMenu(sysMenuDTO));
    }

    /**
     * 获取详情
     */
    @GetMapping("/detail")
    public Response<?> detail(@Valid @RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        return Response.succeed(sysMenuService.getMenuById(id));
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/del")
    public Response<?> del(@Valid @RequestBody List<Integer> ids) {
        sysMenuService.del(ids);
        return Response.succeed();
    }

    /**
     * 菜单树
     */
    @PostMapping("/tree")
    public Response<?> tree(@RequestBody  SysMenuDTO sysMenuDTO) {
        return Response.succeed(sysMenuService.getSysTreeDTO(sysMenuDTO));
    }

    /**
     * 根据获取菜单树(用于角色勾选菜单)
     */
    @GetMapping("/getMenuTreeByRoleId")
    public Response<?> getMenuTreeByRoleId(@Valid @RequestParam(value = "roleId", defaultValue = "", required = false) Integer roleId) {
        return Response.succeed(sysMenuService.getMenuTreeByRoleId(roleId));
    }


    /**
     * 列表
     */
    @PostMapping("/list")
    public Response<?> list(@RequestBody SysMenuDTO sysMenuDTO) {
        return Response.succeed(sysMenuService.listByParam(sysMenuDTO));
    }

    /**
     * 根据用户获取菜单树(用于角色勾选菜单)
     */
    @GetMapping("/getMenuTreeByUserId")
    public Response<?> getMenuTreeByUserId(@Valid @RequestParam(value = "userId", defaultValue = "", required = false) Integer userId,
                                           @Valid @RequestParam(value = "terminal", defaultValue = "", required = false) Integer terminal) {
        return Response.succeed(sysMenuService.getMenuTreeByUserId(userId,terminal));
    }


    /**
     * 根据用户获取按钮权限
     */
    @GetMapping("/getPermsByUserId")
    public Response<?> getPermsByUserId(@Valid @RequestParam(value = "userId", defaultValue = "", required = false) Integer userId) {
        return Response.succeed(sysMenuService.getPermsByUserId(userId));
    }


}
